<?php

namespace app;
/**
 * Class Check
 * Author daichen
 * @package app
 * 检测数据层
 */
class Check
{
    static $validater = null;

    private static function validate()
    {
        if(self::$validater == null){
            self::$validater =  new \vender\Validate();
        }
        return self::$validater;
    }

    /**
     * 验证消息模板推送数据
     *
     * @param $cardNum
     * @param $type
     * @param $obj
     *
     * @return bool|string
     */
    public static function checkSendMessage($cardNum, $type, $obj)
    {
        $data          = array(
            'cardnum' => $cardNum,
            'type'    => $type,
            'obj'     => $obj
        );
        $validate_role = array(
            'cardnum' => array('required' => true),
            'type'    => array('required' => true),
            'obj'     => array('required' => true, 'isjson' => true)
        );
        $validate_mess = array(
            'cardnum' => array('required' => '车牌号码或手机号码不能为空'),
            'type'    => array('required' => '消息类型不能为空'),
            'obj'     => array('required' => 'Json数据不能为空', 'isjson' => 'Json数据不合法')
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 消费成功
     *
     * @param $data
     *
     * @return bool|string
     */
    public static function checkOrder($data)
    {
        $data          = array(
            'name'       => $data['name'],
            'shoppename' => $data['shoppeName'],
//            'category'   => $data['category'],
            'type'       => $data['type'],
//            'retailid'   => $data['retailId'],
            'amount'     => $data['amount'],
            'date'       => $data['date'],
            'score'      => $data['score'],
            'totalscore' => $data['totalScore'],
        );
        $validate_role = array(
            'name'       => array('required' => true),
            'shoppename' => array('required' => true),
//            'category'   => array('required' => true),
//            'retailid'   => array('required' => true),
            'type'       => array('required' => true),
            'amount'     => array('required' => true),
            'date'       => array('required' => true),
            'score'      => array('required' => true, 'number' => true),
            'totalscore' => array('required' => true, 'number' => true),
        );
        $validate_mess = array(
            'name'       => array('required' => '姓名不能为空'),
            'shoppename' => array('required' => '消费门店不能为空'),
            'type'       => array('required' => '消费类型不能为空'),
//            'category'   => array('required' => '品类不能为空'),
//            'retailid'   => array('required' => '流水号不能为空'),
            'amount'     => array('required' => '消费金额不能为空'),
            'date'       => array('required' => '消费时间不能为空'),
            'score'      => array('required' => '积分不能为空', 'number' => '积分必须为数字'),
            'totalscore' => array('required' => '总积分不能为空', 'number' => '总积分必须为数字'),
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 会员注册
     *
     * @param $data
     *
     * @return bool|string
     */
    public static function checkReg($data)
    {
        $data          = array(
            'name'       => $data['name'],
            'cardnumber' => $data['cardNumber'],
            'date'       => $data['date'],
            'level'      => $data['level']
        );
        $validate_role = array(
            'name'       => array('required' => true),
            'cardnumber' => array('required' => true),
            'date'       => array('required' => true),
            'level'      => array('required' => true)
        );
        $validate_mess = array(
            'name'       => array('required' => '姓名不能为空'),
            'cardnumber' => array('required' => '会员卡号不能为空'),
            'date'       => array('required' => '有效日期不能为空'),
            'level'      => array('required' => '会员等级不能为空')
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 会员入场
     *
     * @param $data
     *
     * @return string
     */
    public static function checkEntry($data)
    {
        $data          = array('entrytime' => $data['entryTime']);
        $validate_role = array('entrytime' => array('required' => true));
        $validate_mess = array('entrytime' => array('required' => '入场时间不能为空'));
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 会员离场
     *
     * @param $data
     *
     * @return string
     */
    public static function checkLeave($data)
    {
        $data          = array(
            'leavetime' => $data['leaveTime'],
            'amount'    => $data['amount'],
            'duration'  => $data['duration'],
            'prestore'  => $data['prestore'],
            'score'     => $data['score']
        );
        $validate_role = array(
            'leavetime' => array('required' => true),
            'amount'    => array('required' => true, 'number' => true),
            'duration'  => array('required' => true),
            'prestore'  => array('required' => true),
            'score'     => array('required' => true, 'number' => true)
        );
        $validate_mess = array(
            'leavetime' => array('required' => '离场时间不能为空'),
            'amount'    => array('required' => '停车总金额不能为空', 'number' => '停车总金额必须为数字'),
            'duration'  => array('required' => '停车时长不能为空'),
            'prestore'  => array('required' => '实付金额不能为空'),
            'score'     => array('required' => '消费积分不能为空', 'number' => '积分必须为数字')
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 对积分消费数据验证
     *
     * @param $data
     *
     * @return bool|string
     */
    public static function checkScore($data)
    {
        $data          = array(
            'name'       => $data['name'],
            'cardnumber' => $data['cardNumber'],
            'date'       => $data['date'],
//            'content'    => $data['content'],
            'score'      => $data['score'],
            'totalscroe' => $data['totalScore']
        );
        $validate_role = array(
            'name'       => array('required' => true),
            'cardnumber' => array('required' => true, 'number' => true),
            'date'       => array('required' => true),
//            'content'    => array('required' => true),
            'score'      => array('required' => true, 'number' => true),
            'totalscroe' => array('required' => true, 'number' => true)
        );
        $validate_mess = array(
            'name'       => array('required' => '姓名不能为空'),
            'cardnumber' => array('required' => '会员卡号不能为空', 'number' => '会员卡号必须为数字'),
            'date'       => array('required' => '购物日期不能为空'),
//            'content'    => array('required' => '积分消费内容不能为空'),
            'score'      => array('required' => '消费积分不能为空', 'number' => '积分必须为数字'),
            'totalscroe' => array('required' => '剩余积分不能为空', 'number' => '积分必须为数字')
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }


    /**
     * 积分缴费接口
     *
     * @param $cardNum
     * @param $amount
     * @param $recordId
     *
     * @return bool|string
     */
    public static function checkScorePay($cardNum, $amount)
    {
        $data          = array(
            'cardnum' => $cardNum,
            'amount'  => $amount
        );
        $validate_role = array(
            'cardnum' => array('required' => true),
            'amount'  => array(
                'required' => true,
                'number'   => true
            )
        );
        $validate_mess = array(
            'cardnum' => array('required' => '车牌号码或手机号码不能为空'),
            'amount'  => array(
                'required' => '停车费总金额不能为空',
                'number'   => '停车费总金额必须为整数'
            )
        );
        $validate      = self::validate()->verify($data, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }
    }

    /**
     * 停车位信息校验
     *
     * @param $data 车位信息
     *
     */
    public static function checkParking($data)
    {
        $vldata = array(
            'parkingNo' => $data['parkingNo'],
            'parkArea'  => $data['parkArea'],
            'floor'     => $data['floor'],
            'entryTime' => $data['entryTime'],
            //'parkingImg'=> $data['parkingImg'],
        );

        $validate_role = array(
            'parkingNo'     => array('required' => true),
            'parkArea'      => array('required' => true),
            'floor'         => array('required' => true),
            'entryTime'     => array('required' => true),
            //'parkingImg'     => array('required' => true)
        );

        $validate_mess = array(
            'parkingNo'     => array('required' => '停车位编号不能为空'),
            'parkArea'      => array('required' => '停车位区域不能为空'),
            'floor'         => array('required' => '停车楼层不能为空'),
            'entryTime'     => array('required' => '入场时间不能为空'),
            //'parkingImg'     => array('required' => '停车位照片不能为空')
        );

        $validate      = self::validate()->verify($vldata, $validate_role, $validate_mess);

        if($validate !== true){
            return json('0',$validate);
        }

    }
}